package com.chzc.fm.duotin.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.HotAnLinearLayoutAdapter;
import com.chzc.fm.duotin.bean.channel.AnchorSpecial;
import com.chzc.fm.duotin.bean.channel.AnchorSpecialItem;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class HostStationActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private ImageView ivback;
    private TextView tvtitle;
    private HotAnLinearLayoutAdapter adapter;

    private AnchorSpecial special;
    private AnchorSpecialItem specialItem;
    private String dataid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_hoststation);
        initeViews();
        initDatas();
    }

    public void initeViews()
    {
        recyclerView = (RecyclerView) findViewById(R.id.hoststation_content_rv);
        ivback = (ImageView) findViewById(R.id.hoststation_back_iv);
        tvtitle = (TextView) findViewById(R.id.hoststation_title_tv);

        LinearLayoutManager manager = new LinearLayoutManager(HostStationActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        ivback.setOnClickListener(this);
        tvtitle.setOnClickListener(this);
    }

    public void initDatas()
    {
        special = (AnchorSpecial) getIntent().getSerializableExtra("special");
        specialItem = (AnchorSpecialItem) getIntent().getSerializableExtra("specialitem");

        if(special != null){
            tvtitle.setText(special.getTitle());
            dataid = special.getItem_value();
        }else
        {
            tvtitle.setText(specialItem.getName());
            dataid = specialItem.getId()+"";
        }



        getTalkShow(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {

                    //解析
                    LogUtil.w("开始解析数据");

                    JSONObject root = new JSONObject(object.toString());

                    String error_msg = root.getString("error_msg");

                    if("success".equals(error_msg)) {

                        JSONObject data = root.getJSONObject("data");

                        JSONArray showlist = data.getJSONArray("data_list");

                        List<AnchorSpecial> specialList = AnchorSpecial.arrayAnchorSpecialFromData(showlist.toString());

                        LogUtil.w("specialList.size()" + specialList.size());

                         adapter = new HotAnLinearLayoutAdapter(HostStationActivity.this,specialList);
                        recyclerView.setAdapter(adapter);
                        adapter.setOnItemClickListener(new HotAnLinearLayoutAdapter.mOnItemClickListener() {
                            @Override
                            public void OnItemClick(int position) {

                            }
                        });
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }



    /**
     * 请求脱口秀数据
     * @param callback
     */
    public void getTalkShow(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                LogUtil.w("GlobalParam.URL_TALKSHOW_HEAD + special.getItem_value() " +
                        "+ GlobalParam.URL_TALKSHOW_TAIL>>" +
                        GlobalParam.URL_TALKSHOW_HEAD + dataid +
                        GlobalParam.URL_TALKSHOW_TAIL);
                return HttpUtil.doGet(GlobalParam.URL_TALKSHOW_HEAD + dataid
                        + GlobalParam.URL_TALKSHOW_TAIL);
            }
        };
        new DuotinTask(request,callback).execute();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.hoststation_back_iv:
                finish();
                break;
            case R.id.hoststation_title_tv:
                Intent intent = new Intent(HostStationActivity.this,TypeAnchorActivity.class);
                startActivity(intent);
                break;
        }
    }
}
